<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司代码" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入公司代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编码" prop="stockCode">
        <el-input
          v-model="queryParams.stockCode"
          placeholder="请输入仓库编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库区编码" prop="kuquCode">
        <el-input
          v-model="queryParams.kuquCode"
          placeholder="请输入库区编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位编码" prop="kuweiCode">
        <el-input
          v-model="queryParams.kuweiCode"
          placeholder="请输入库位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位简码" prop="kuweiJmCode">
        <el-input
          v-model="queryParams.kuweiJmCode"
          placeholder="请输入库位简码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:warehouseinfoKuwei:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleCopy"
          v-hasPermi="['base:warehouseinfoKuwei:copy']"
        >复制</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:warehouseinfoKuwei:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:warehouseinfoKuwei:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:warehouseinfoKuwei:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-printer"
          size="mini"
          :disabled="multiple"
          @click="printPi"
        >批量打印</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-printer"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="printPi2"-->
<!--        >批量打印</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehouseinfoKuweiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" width="100"  align="center" prop="tenantId" />
      <el-table-column label="仓库编码" width="150" align="center" prop="stockCode" />
      <el-table-column label="仓库名称" width="150" align="center" prop="stockName" />
      <el-table-column label="库区编码" width="150" align="center" prop="kuquCode" />
      <el-table-column label="库区名称" width="150" align="center" prop="kuquName" />
      <el-table-column label="库位编码" width="150" align="center" prop="kuweiCode" />
      <el-table-column label="库位名称" width="150" align="center" prop="kuweiName" />
      <el-table-column label="库位简码" width="150" align="center" prop="kuweiJmCode" />
      <el-table-column label="管理部门代码" width="100" align="center" prop="deptId" />
      <el-table-column label="库位类型"  align="center" prop="kuweiType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kuwei_type" :value="scope.row.kuweiType"/>
        </template>
      </el-table-column>
      <el-table-column label="出库管理"  align="center" prop="ckState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stock_state" :value="scope.row.ckState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:warehouseinfoKuwei:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:warehouseinfoKuwei:remove']"
          >删除</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-printer"-->
<!--            @click="printKW(scope.row)"-->
<!--          >打印</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库位管理对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
       <!-- <el-form-item label="公司代码" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入公司代码" />
        </el-form-item>-->
   <!--     <el-form-item label="公司名称" prop="tenantId">
          <el-select v-model="form.tenantId" placeholder="请选择公司名称">
            <el-option
              v-for="item in gcCodeList"
              :key="item.jtdm"
              :label="item.jtmc"
              :value="item.jtdm">
              <span style="float: left">{{ item.jtmc }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.jtdm }}</span>
            </el-option>
          </el-select>
        </el-form-item>-->
        <!--<el-form-item label="仓库编码" prop="stockCode">
          <el-input v-model="form.stockCode" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入仓库名称" />
        </el-form-item>-->
        <el-form-item label="所属仓库" prop="stockCode">
          <el-select @change="getWarehouseInfo" filterable  v-model="form.stockCode" placeholder="请选择仓库" clearable>
            <el-option
              v-for="item in warehouseinfoList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库区编码" prop="kuquCode">
          <el-input v-model="form.kuquCode" placeholder="请输入库区编码" />
        </el-form-item>
        <el-form-item label="库区名称" prop="kuquName">
          <el-input v-model="form.kuquName" placeholder="请输入库区名称" />
        </el-form-item>
        <el-form-item label="库位编码" prop="kuweiCode">
          <el-input v-model="form.kuweiCode" placeholder="请输入库位编码" />
        </el-form-item>
        <el-form-item label="库位名称" prop="kuweiName">
          <el-input v-model="form.kuweiName" placeholder="请输入库位名称" />
        </el-form-item>
        <el-form-item label="库位简码" prop="kuweiJmCode">
          <el-input v-model="form.kuweiJmCode" placeholder="请输入库位简码" />
        </el-form-item>
       <!-- <el-form-item label="管理部门代码" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入管理部门代码" />
        </el-form-item>-->
        <el-form-item label="管理部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择管理部门" />
        </el-form-item>
        <el-form-item label="库位类型" prop="kuweiType">
          <el-select v-model="form.kuweiType" placeholder="请选择库位类型">
            <el-option
              v-for="dict in dict.type.kuwei_type"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出库管理" prop="ckState">
          <el-select v-model="form.ckState" placeholder="请选择出库管理">
            <el-option
              v-for="dict in dict.type.stock_state"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div style="width: 100%;display: none">
      <div style="width: 100%;padding-top:30px;font-size: 20px;" ref="printDan">
        <div v-for="(item,index) in jms2" :key="index" style="width: 50%;text-align: center;display: block;float: left;margin-bottom: 20px" >
          <div style="display: inline-block;margin-bottom: 10px" class="qrcode" ref="qrCodeUrl"></div>
          <div>{{item}}</div>
        </div>
      </div>
    </div>
<!--    <div style="width: 100%;display: flex">-->
<!--      <div  class="qrcode" ref="qrCodeUrl" style="margin-top: 20px;margin-left: 10px;width: 50%"></div>-->
<!--    </div>-->
  </div>
</template>

<script>
import { listWarehouseinfoKuwei, getWarehouseinfoKuwei, delWarehouseinfoKuwei, addWarehouseinfoKuwei, updateWarehouseinfoKuwei } from "@/api/base/warehouseinfoKuwei";
import { getListWarehouseinfo } from "@/api/base/warehouseinfo";
import {listAllOrgManage} from "@/api/hr/orgManage";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getLodop} from "@/common/LodopFuncs";
import QRCode from "qrcodejs2";
import print from 'print-js';

export default {
  name: "WarehouseinfoKuwei",
  dicts: ['stock_state', 'kuwei_type'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      jms:[],
      jms2:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库位管理表格数据
      warehouseinfoKuweiList: [],
      warehouseinfoList: [],
      // 部门树选项
      deptOptions: undefined,
      gcCodeList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        stockCode: null,
        kuquCode: null,
        kuweiCode: null,
        kuweiJmCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stockCode: [
          { required: true, message: "仓库不能为空", trigger: "blur" }
        ],
        tenantId: [
          { required: true, message: "公司代码不能为空", trigger: "blur" }
        ],
        kuquCode: [
          { required: true, message: "库区编码不能为空", trigger: "blur" }
        ],
        kuweiCode: [
          { required: true, message: "库位编码不能为空", trigger: "blur" }
        ],
        kuweiType: [
          { required: true, message: "库位类型不能为空", trigger: "blur" }
        ],
        ckState: [
          { required: true, message: "出库管理不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getWarehouseinfoList();
    this.getTreeselect();
    listAllOrgManage().then(response => {
      this.gcCodeList = response.rows;
    });
  },
  methods: {
    // 打印库位简码
    printKW(row){
      console.log(row.stockCode)
      let jm = row.stockCode
      let kq = row.kuquCode
      let code = row.stockCode+"@"+row.kuquCode
      console.log(code,'aaaaaaaaaaaaaa')
      let LODOP = getLodop();
      LODOP.PRINT_INIT("");
      LODOP.PRINT_INITA(0,0,190,94,"");
      LODOP.SET_PRINT_PAGESIZE(1,1000,500,"");
      LODOP.ADD_PRINT_BARCODE(7,4,370,58,"PDF417",code);
      LODOP.SET_PRINT_STYLEA(0,"FontSize",18);
      LODOP.ADD_PRINT_BARCODE(66,206,130,130,"QRCode",code);
      LODOP.ADD_PRINT_TEXT(86,80,150,100,jm);
      LODOP.SET_PRINT_STYLEA(0,"FontName","黑体");
      LODOP.SET_PRINT_STYLEA(0,"FontSize",20);
      LODOP.SET_PRINT_STYLEA(0,"Bold", 2);
        //    LODOP.PRINT();
      LODOP.PREVIEW();
      // LODOP.PRINT_DESIGN()
    },
    //批量打印
    printPi(){
      // console.log(this.jms)
      // console.log(this.jms,'222')
      let LODOP = getLodop();
      LODOP.PRINT_INIT("");
      LODOP.PRINT_INITA(0,0,190,94,"");
      for(let i=0;i<this.jms.length;i++){

        LODOP.NewPage();
        LODOP.SET_PRINT_PAGESIZE(1,500,250,"");
        // LODOP.ADD_PRINT_BARCODE(7,4,186,35,"PDF417",this.jms[i].kuweiJmCode);
        LODOP.SET_PRINT_STYLEA(0,"FontSize",12);
        //LODOP.ADD_PRINT_BARCODE(46,126,170,50,"QRCode",this.jms[i].kuweiJmCode);
        LODOP.ADD_PRINT_BARCODE(26,110,74,74,"QRCode",this.jms[i].kuweiJmCode);
        LODOP.ADD_PRINT_TEXT(26,2,110,41,this.jms[i].kuweiJmCode);
        LODOP.SET_PRINT_STYLEA(0,"FontName","黑体");
        LODOP.SET_PRINT_STYLEA(0,"FontSize",14);
        LODOP.SET_PRINT_STYLEA(0,"Bold", 2);
        //    LODOP.PRINT();

      }
      LODOP.PREVIEW();
    },
    //批量打印
    printPi2(){
      this.$nextTick(()=>{
        this.jms2.forEach((item,index)=>{
          let code = item;
          this.$refs.qrCodeUrl[index].innerHTML="";
          new QRCode(this.$refs.qrCodeUrl[index], {
            text: code, //二维码内容
            width: 150,
            height: 150,
            // colorDark: "#333333", //二维码颜色
            // colorLight: "#ffffff", //二维码背景色
          })
        })
        setTimeout(()=>{
          this.priDan()
        },200)
      })

      // let LODOP = getLodop();
      // LODOP.PRINT_INIT("");
      // LODOP.PRINT_INITA(0,0,190,94,"");
      // for(let i=0;i<this.jms.length;i++){
      //
      //   LODOP.NewPage();
      //   LODOP.SET_PRINT_PAGESIZE(1,500,250,"");
      //   LODOP.ADD_PRINT_BARCODE(7,4,186,35,"PDF417",this.jms[i]);
      //   LODOP.SET_PRINT_STYLEA(0,"FontSize",18);
      //   LODOP.ADD_PRINT_BARCODE(46,126,170,50,"QRCode",this.jms[i]);
      //   LODOP.ADD_PRINT_TEXT(56,2,130,41,this.jms[i]);
      //   LODOP.SET_PRINT_STYLEA(0,"FontName","黑体");
      //   LODOP.SET_PRINT_STYLEA(0,"FontSize",20);
      //   LODOP.SET_PRINT_STYLEA(0,"Bold", 2);
      //
      // }
      // LODOP.PREVIEW();
    },
    priDan(){

      const style = '@page { margin:0 } ' +'@media print { #title{ font-size:40px};#title1{ font-size:40px }  ' ;
      printJS({
        printable:this.$refs.printDan,
        type:'html',
        header:'',
        targetStyles:['*'],
        scanStyles: false,
        style:style,
        onPrintDialogClose: () => {
          console.log('弹窗关闭');
        },
        onLoadingEnd:()=>{
          console.log('加载完毕')
        }
      })
    },
    /** 查询库位管理列表 */
    getList() {
      this.loading = true;
      listWarehouseinfoKuwei(this.queryParams).then(response => {
        this.warehouseinfoKuweiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWarehouseinfoList(){
      getListWarehouseinfo().then(response => {
        this.warehouseinfoList = response.data;
      });
    },
    getWarehouseInfo(value){
      let selectedName = {};
      selectedName = this.warehouseinfoList.find((item)=>{
        return item.value === value;
        //筛选出匹配数据，是对应数据的整个对象
      });
      this.form.stockName=selectedName.label;
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        stockCode: null,
        stockName: null,
        kuquCode: null,
        kuquName: null,
        kuweiCode: null,
        kuweiName: null,
        kuweiJmCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      //this.jms = selection.map(item => item.stockCode)
      this.jms = selection
      this.jms2 = selection.map(item => item.kuweiJmCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库位管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWarehouseinfoKuwei(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位管理";
      });
    },
    handleCopy(row){
      this.reset();
      const id = row.id || this.ids
      getWarehouseinfoKuwei(id).then(response => {
        this.form = response.data;
        this.form.id=null;
        this.open = true;
        this.title = "复制库位管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWarehouseinfoKuwei(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouseinfoKuwei(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库位管理编号为"' + ids + '"的数据项？').then(function() {
        return delWarehouseinfoKuwei(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/warehouseinfoKuwei/export', {
        ...this.queryParams
      }, `warehouseinfoKuwei_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
